package com.huadao.cart_ccs.experiment.confirmBeforeCulture.rawMaterial;

import android.text.TextUtils;

import com.huadao.cart_ccs.common.TimeUtil;
import com.huadao.cart_ccs.data.bean.Dispensing;
import com.huadao.cart_ccs.data.bo.RawMaterialBO;
import com.huadao.cart_ccs.data.source.http.RockBaseObserver;
import com.huadao.cart_ccs.modle.DispensingModle;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class RawMaterialPresenter extends IRawMaterialContract.IRawMaterialPresenter {

    private DispensingModle dispensingModle;
    private boolean isSearch;
    private Disposable checkDataDisposable;
    public RawMaterialPresenter(IRawMaterialContract.IRawMaterialView baseView) {
        attachView(baseView);
        dispensingModle=new DispensingModle(baseView.getContext());
    }

    @Override
    public void getRawMaterialList() {
//        List<Dispensing> data=new ArrayList<>();
//        data.add(new Dispensing());
//        data.add(new Dispensing());
//        data.add(new Dispensing());
//        data.add(new Dispensing());
//        data.add(new Dispensing());
//        baseView.onRawMaterialList(data);

        isSearch=false;
        Observable<List<Dispensing>> observable= dispensingModle.getDispensing(baseView.getProjectId());
        dispensingModle.toSubscribe(observable, new RockBaseObserver<List<Dispensing>>(this,null) {
            @Override
            protected void onBaseNext(List<Dispensing> data) {
                baseView.onRawMaterialList(data);
            }
        });
    }

    @Override
    public void getMaterialListByDay(final int day) {
        isSearch=false;
        Observable<List<Dispensing>> observable= dispensingModle.getDispensing(baseView.getProjectId());
        dispensingModle.toSubscribe(observable, new RockBaseObserver<List<Dispensing>>(this,null) {
            @Override
            protected void onBaseNext(List<Dispensing> data) {
                Calendar projectStartTime=Calendar.getInstance();
                projectStartTime.setTime(baseView.getProjectStartTime());

                Date startDate;
                Date endDate;
                if(day==1){
                    startDate=projectStartTime.getTime();
                    projectStartTime.set(Calendar.HOUR_OF_DAY,0);
                    projectStartTime.set(Calendar.MINUTE,0);
                    projectStartTime.set(Calendar.SECOND,0);
                    projectStartTime.add(Calendar.DAY_OF_MONTH,1);
                    endDate=projectStartTime.getTime();
                }else {
                    projectStartTime.set(Calendar.HOUR_OF_DAY,0);
                    projectStartTime.set(Calendar.MINUTE,0);
                    projectStartTime.set(Calendar.SECOND,0);
                    projectStartTime.add(Calendar.DAY_OF_MONTH,day-1);
                    startDate=projectStartTime.getTime();
                    projectStartTime.add(Calendar.DAY_OF_MONTH,1);
                    endDate=projectStartTime.getTime();
                }
                List<Dispensing> dispensingList=getDispensingBetween(data,startDate,endDate);
                baseView.onRawMaterialList(dispensingList);
            }
        });
    }

    @Override
    public void refresh() {
        if(isSearch){
            search();
        }else {
            int day = baseView.getSelectedDay();
            if (day == 0) {
                getRawMaterialList();
            } else {
                getMaterialListByDay(day);
            }
        }
    }

    @Override
    void checkDataSchedual() {
        checkDataDisposable=Observable.interval(3,1,TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        refresh();
//                        if(isSearch){
//                            search();
//                        }else {
//                            if(baseView.getSelectedDay()==-1){
//                                getRawMaterialList();
//                            }else {
//                                getMaterialListByDay(baseView.getSelectedDay());
//                            }
//                        }
                    }
                });
    }

    private List<Dispensing> getDispensingBetween(List<Dispensing> data, Date startDate, Date endDate){
        List<Dispensing> dispensingList=new ArrayList<>();
        for(Dispensing dispensing:data){
            if(dispensing.getUseTime()!=null&&dispensing.getUseTime().after(startDate)&&dispensing.getUseTime().before(endDate)){
                dispensingList.add(dispensing);
            }
        }
        return dispensingList;
    }

    @Override
    public void search() {
        isSearch=true;
        String name=baseView.getSearchWord();
        Observable<List<Dispensing>> observable= dispensingModle.searchDispensingBykeyWord(baseView.getProjectId(),name);
        dispensingModle.toSubscribe(observable, new RockBaseObserver<List<Dispensing>>(this,null) {
            @Override
            protected void onBaseNext(List<Dispensing> data) {
                baseView.onSearchRawMaterialList(data);
            }
        });
    }

    @Override
    public void detachView() {
        if(checkDataDisposable!=null){
            checkDataDisposable.dispose();
        }
        super.detachView();
    }
}
